<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ page import="com.broadvision.data.client.DataManager" %>
<%@ page import="com.broadvision.emarketing.action.segment.SegmentConstants" %>
<%--
------------------------------Note----------------------------
In order to include this page, you need have the following
request attribute defined: <br>
segmentList, which represents the segment list.

--%>

<c:set var="viewBean" value="${emSegmentBrowseView}" />
<c:set var="viewBeanMap" value="${viewBean.map}" />
<c:set var="editIcon">
  <html:img   page="/mc/images/action/icon_edit.gif" 
    width="15" height="15" border="0" styleClass="actionIcon"
    altKey="em.image.alt.edit" titleKey="em.image.alt.edit" bundle="em"/>
</c:set>
<c:set var="deleteIcon">
  <html:img page="/mc/images/action/icon_delete.gif"
    width="15" height="15" border="0" styleClass="actionIcon"
    altKey="em.image.alt.delete" titleKey="em.image.alt.delete" bundle="em"/>
</c:set>
<c:set var="onlineIcon">
  <html:img page="/mc/images/action/icon_online.gif"
    width="15" height="15" border="0" styleClass="actionIcon"
    altKey="em.image.alt.setOnline" titleKey="em.image.alt.setOnline" bundle="em"/>
</c:set>
<c:set var="offlineIcon">
  <html:img page="/mc/images/action/icon_offline.gif"
    width="15" height="15" border="0" styleClass="actionIcon"
    altKey="em.image.alt.setOffline" titleKey="em.image.alt.setOffline" bundle="em"/>
</c:set>
<script language="javascript">
  function check(field, button) {
    var SelectAllTxt = '<bean:message key="button.selectAll" bundle="mc.messages"/>';
    var UnselectAllTxt = '<bean:message key="button.unselectAll" bundle="em"/>';
    if (field == null) {
      button.value = SelectAllTxt;
    }
    var checkflag = "false";
    if (button.value == SelectAllTxt) {
      checkflag = "false";
    }
    if (button.value == UnselectAllTxt) {
      checkflag = "true";
    }
    var max = field.length;
    if (max > 1) {
      if (checkflag == "false") {
        for (i = 0; i < max; i++) {
          field[i].checked = true;
        }
        button.value = UnselectAllTxt;
      } else {
        for (i = 0; i < max; i++) {
          field[i].checked = false;
        }
        button.value = SelectAllTxt;
      }
    } else {
      if (checkflag == "false") {
        field.checked = true;
        button.value = UnselectAllTxt;
      } else {
        field.checked = false;
        button.value = SelectAllTxt;
      }
    }
  }
  
  function createNewSegment(theForm, action) {
    theForm.action = '<html:rewrite page="'+action+'" />'; 
    theForm.submit();
    return false;
  }

  function callBackForSegmentMove(catId, catFullPath) {
    // Add code according to your need
    var theForm = document.emSegmentDeleteForm;

    theForm.newParentCategoryOid.value = catId;
    theForm.action = '<html:rewrite page="/em/segment/segmentMove.do" />';
    theForm.submit();
  }


  function confirmSegmentBatchMove(myform)
  {
    var count = 0;
    if (myform.segmentOid == null) {
      //There is no segment under this category.
      return false;
    }

    //There is at least one segment in this category.
    if ( myform.segmentOid.length == null ) {
      //There is only one segment under this category.
      if ( myform.segmentOid.checked == true ) {
        count++;
      }
    } else {
      for (i = 0; i < myform.segmentOid.length; i++) {
        if ( myform.segmentOid[i].checked == true ) {
          count++;
        }
      }
    }

    if (count == 0) {
      //The user doesn't select any segment
      alert( "<bean-el:message bundle="em" key="em.segment.browse.js.alert.selectSegment"/>")
      return false;
    } else {
      //The user selects at least one segment.
      <%
        final int segmentCntTypeId = DataManager.getContentTypeId(SegmentConstants.SEGMENT_CONTENT_TYPE_NAME);
      %>
      doPickerPopupAction('<html:rewrite page="/mt/categoryPickerPopup.do"/>', 'callBackForSegmentMove', <%=segmentCntTypeId%>, '<c:out value="${emSegmentBrowseForm.map.currentCategoryOid}" />', null, true, false, false);
      return false;
    }
  }


  function confirmSegmentBatchDelete(myform)
  {
    var count = 0;
    if (myform.segmentOid == null) {
      //There is no segment under this category.
      return false;
    }

    //There is at least one segment in this category.
    if ( myform.segmentOid.length == null ) {
      //There is only one segment under this category.
      if ( myform.segmentOid.checked == true ) {
        count++;
      }
    } else {
      for (i = 0; i < myform.segmentOid.length; i++) {
        if ( myform.segmentOid[i].checked == true ) {
          count++;
        }
      }
    }

    if (count == 0) {
      //The user doesn't select any segment
      alert( "<bean-el:message bundle="em" key="em.segment.browse.js.alert.selectSegment"/>")
      return false;
    } else {
      //The user selects at least one segment.
      return confirmSegmentDelete(count);
    }
  }

  function confirmSegmentDelete(count) {
    if (count == 0)
      return false;

    if (count == 1)
      return confirm("<bean-el:message bundle="em" key="em.segment.browse.js.alert.deleteSegment"/>");
    else
      return confirm("<bean-el:message bundle="em" key="em.segment.browse.js.alert.deleteSegments"/>");
  }

  // function to POST form parameters to a ".do"
  function doAction(action, myform) {
    myform.action = '<html:rewrite page="'+action+'" />';   
    myform.submit();
    return false;
  }

</script>

<html:form action="/em/segment/segmentDelete.do">
  <input type=hidden name=newParentCategoryOid> <%-- for moving segment. --%>
  <c:if test="${not empty segmentList}">
    <div class="globalButtonWrap">
    
     <!-- if no permit to delete and move segment, disable select all button -->
           <c:choose>
             <c:when test="${viewBeanMap.deleteAllType || viewBeanMap.moveType}">
              <input type="button" name="selectAll" class="globalButton" 
              onClick="return check(this.form.segmentOid, this);"
               value="<bean-el:message bundle="em" key="button.selectAll"/>">
             </c:when>
            <c:otherwise>
            </c:otherwise>
      </c:choose>  
   
      <!-- if no permit to delete segment, disable delete button -->
      <c:choose>
             <c:when test="${viewBeanMap.deleteAllType}">
              <input type="button" name="deleteBtn" class="globalButton"
               onClick="if(confirmSegmentBatchDelete(this.form)) doAction('/em/segment/segmentDelete.do', this.form);"
               value="<bean-el:message bundle="em" key="button.delete"/>"> </c:when>
            <c:otherwise>
            </c:otherwise>
      </c:choose>  
          
      <!-- if no permit to move segment, disable move button -->
      <c:choose>
         <c:when test="${viewBeanMap.moveType}">
          <input type="button" name="moveBtn" class="globalButton"
            onClick="return confirmSegmentBatchMove(this.form);"
             value="<bean-el:message bundle="em" key="button.move"/>">
         </c:when>
         <c:otherwise>
         </c:otherwise>
      </c:choose> 
    </div>
  </c:if>

  <table class="dataTable" border="0" cellspacing="1" cellpadding="2" width="100%">
    <tr class="listHeader">
      <th id="Checkboxes" width="5%">&nbsp;</th>
      <th id="Name" align="left" width="20%">
        <bean-el:message bundle="mc.messages" key="mc.header.name"/>
      </th>
      <th id="Description" align="left" width="30%">
        <bean-el:message bundle="mc.messages" key="mc.header.description"/>
      </th>
      <th id="Status" align="left" width="10%">
        <bean-el:message bundle="mc.messages" key="mc.header.status"/>
      </th>
      <th id="LastModified" width="20%" align="left">
        <bean-el:message bundle="mc.messages" key="mc.header.lastModifiedDate"/>
      </th>
      <th id="Actions" align="left" width="15%" nowrap>
        <bean-el:message bundle="mc.messages" key="mc.header.actions"/>
      </th>
    </tr>

    <!-- results -->
    <c:if test="${empty segmentList}">
      <tr class="listEven">
        <td colspan="6" align="center">
          <bean-el:message key="em.segment.browse.no.segment" bundle="em" />
        </td>
      </tr>
    </c:if>
    <c:forEach var="segment" items="${segmentList}" varStatus="index">
      <c:set var="rowStyle">
        <c:choose>
          <c:when test="${index.count % 2 == 0}">
            listEven
          </c:when>
          <c:otherwise>
            listOdd
          </c:otherwise>
        </c:choose>
      </c:set>
      <tr class="<c:out value="${rowStyle}" />" >
        <td id="Checkboxes" width="5%" align="center">
          <input type="checkbox" name="segmentOid" 
            value="<c:out value="${segment.map.oid}" />">
        </td>
        <td id="Name" align="left" width="20%">
        
        <!-- if no permit to view segment, disable view link -->
        <c:choose>
         <c:when test="${segment.map.viewType}">
            <c:url var="segmentViewUrl" value="/em/segment/segmentView.do">
             <c:param name="segmentOid" value="${segment.map.oid}"/>
            </c:url>
           <a href="<c:out value="${segmentViewUrl}" />"><c:out value="${segment.map.name}"/></a>
         </c:when>
         <c:otherwise>
            <c:out value="${segment.map.name}"/>
         </c:otherwise>
        </c:choose>  
        </td>
        
        <td id="Description" align="left" width="30%">
          <c:out value="${segment.map.description}"/>
        </td>
        <td id="Status" align="left" width="10%">
          <c:choose>
            <c:when test="${segment.map.status == 1}">
              <bean-el:message bundle="em" key="em.label.online" />
            </c:when>
            <c:otherwise>
              <bean-el:message bundle="em" key="em.label.offline" />
            </c:otherwise>
          </c:choose>
        </td>
        <td id="LastModified" width="20%" align="left">
          <bean:write name="segment" property="lastModified" bundle="mc.messages" formatKey="format.date.medium"/>
        </td>
        <td id="Actions" align="left" width="15%" nowrap>
        
       <!-- if no permit to edit segment, disable edit icon -->
       <c:choose>
         <c:when test="${segment.map.editType}">
          <html:link paramName="segment" paramProperty="oid"
            paramId="segmentOid" page="/em/segment/segmentEdit.do">
            <c:out value="${editIcon}" escapeXml="false" />
          </html:link>
         </c:when>
         <c:otherwise>
         </c:otherwise>
        </c:choose>  
 
 
    <!-- if no permit to delete segment, disable delete icon -->
       <c:choose>
         <c:when test="${segment.map.deleteType}">
          <html:link  paramName="segment" paramProperty="oid"
            paramId="segmentOid" page="/em/segment/segmentDelete.do"
            onclick="return confirmSegmentDelete(1);">
            <c:out value="${deleteIcon}" escapeXml="false" />
          </html:link>
         </c:when>
         <c:otherwise>
         </c:otherwise>
        </c:choose>  

 	<!-- if no permit to edit segment, disable online/offline icon -->
       <c:choose>
         <c:when test="${segment.map.editType}">
           <c:choose>
           <c:when test="${segment.map.status == 1}">
             <html:link paramName="segment" paramProperty="oid"
               paramId="segmentOid" page="/em/segment/segmentOffline.do">
               <c:out value='${offlineIcon}' escapeXml="false"/></a>
             </html:link>
           </c:when>
           <c:otherwise>
             <html:link paramName="segment" paramProperty="oid"
              paramId="segmentOid" page="/em/segment/segmentOnline.do">
              <c:out value='${onlineIcon}' escapeXml="false"/></a>
             </html:link>
           </c:otherwise> 
           </c:choose>  
         </c:when>
         <c:otherwise>
         </c:otherwise>
        </c:choose>             
        </td>
      </tr>
    </c:forEach>
  </table>
</html:form>
<c:if test="${not empty segmentList}">
  <jsp:include page="/mc/common/pager.jsp" />
</c:if>
